package com.mldkhn.led_controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import com.larswerkman.colorpicker.ColorPicker;
import com.larswerkman.colorpicker.ColorPicker.OnColorChangedListener;
import com.larswerkman.colorpicker.SaturationBar;
import com.larswerkman.colorpicker.ValueBar;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

@SuppressLint("DefaultLocale")
public class Fragment_Fade extends Fragment implements OnColorChangedListener, OnClickListener, OnSeekBarChangeListener {
	
	private static Button btnFadeStart, btnFadeStop;
	private static ColorPicker picker;
	private static SaturationBar saturationBar;
	private static ValueBar valueBar;
	private static TextView lblFadeDelay;
	private static SeekBar scrFadeDelay;
	private static String strFade = "FADE";
	private static String strStop = "OFF";
	private int progressDelay = 0, color = 0;
	private String colorHEX;
	
	private static int SERVERPORT;
	private static String SERVER_IP;
	private Socket socket;
	Thread socketThread;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	    View v = inflater.inflate(R.layout.fragment_fade, null);
	    picker = (ColorPicker) v.findViewById(R.id.colorPickerFade);
	    saturationBar = (SaturationBar) v.findViewById(R.id.saturationBarFade);
	    valueBar = (ValueBar) v.findViewById(R.id.valueBarFade);
	    lblFadeDelay = (TextView) v.findViewById(R.id.lblFadeDelay);
	    lblFadeDelay.setText(" 0.0 s");
	    scrFadeDelay = (SeekBar) v.findViewById(R.id.scrFadeDelay);
	    scrFadeDelay.setOnSeekBarChangeListener(this);
	    btnFadeStart = (Button) v.findViewById(R.id.btnFadeStart);
	    btnFadeStop = (Button) v.findViewById(R.id.btnFadeStop);
	    btnFadeStart.setOnClickListener(this);
	    btnFadeStop.setOnClickListener(this);
	    
	    picker.addSaturationBar(saturationBar);
	    picker.addValueBar(valueBar);
		try{
			SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
			SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
			SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
			socketThread = new Thread(new ClientThread());
			socketThread.start();
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())),
					true);
			out.println("HELLO");
			out.close();
			socketThread.interrupt();
			socketThread = null;
			
			} 
			  catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
	    return v;
		}

	@Override
	public void onClick(View v) {
				switch(v.getId()){
				case R.id.btnFadeStart:
					color = picker.getColor();
					colorHEX = Integer.toHexString(color);
					picker.setOldCenterColor(picker.getColor());
					try{
						SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
						SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
						SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
						socketThread = new Thread(new ClientThread());
						socketThread.start();
						PrintWriter out = new PrintWriter(new BufferedWriter(
								new OutputStreamWriter(socket.getOutputStream())),
								true);
						String Delay;
						Delay = String.format("%02d", progressDelay);
						out.println(strFade + "\n" + colorHEX + "\n" + Delay);
						out.close();
						socketThread.interrupt();
						socketThread = null;
						
						} 
						  catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}

		            break;
		        case R.id.btnFadeStop:
					try{
						SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
						SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
						SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
						socketThread = new Thread(new ClientThread());
						socketThread.start();
						PrintWriter out = new PrintWriter(new BufferedWriter(
								new OutputStreamWriter(socket.getOutputStream())),
								true);
						out.println(strStop);
						socketThread.interrupt();
						socketThread = null;
						
						} 
						  catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
		        	
				}
			}
		

	@Override
	public void onColorChanged(int color) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		switch(seekBar.getId()){
		case R.id.scrFadeDelay:
			lblFadeDelay.setText(" " + (float)progress/2 + " s");
			progressDelay = progress;
			break;
	}
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub
		
	}
	
	class ClientThread implements Runnable {
		
		@Override
		public void run() {
			
			try {
				InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

				socket = new Socket(serverAddr, SERVERPORT);

			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
